$(function() {
    var cols=6;
	var rows_delete=0;
	var mode="add";
	var m_id=0;
    var url='controls/other_register_model.php';
    var cboObj;
	var objSelectTable=[];
	
	initialData();
	
	 $("#txtAmt").live('keypress',function(e){

		if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){
				return false;
		}
    });
	 function initialData(){
		$.get(url, {"m": "term"}, function(data) {
			var obj = eval('(' + data + ')');
			if (obj != null) {
				$.each(obj, function(index, text) {
					$("#txtTerm").append(
							$('<option></option>').val(index).html(text)
							);
				});
				$.get(url,{"m":"currterm"},function(data){
					$("#txtTerm").val(data);
				});
			}
		}); 
		$.get(url, {"m":"year"},function(data){
			var cboObj=eval('('+data+')');	
			if (cboObj != null){
				$.each(cboObj, function(index, text) {
					$("#cboYear").append(
						$('<option></option>').val(text).html(text)
					);
				});
				$.get(url,{"m":"curryear"},function(data){
					$("#cboYear").val(data);
				});
			}
		 
		});
		$.get(url, {"m":"class"},function(data){
			cboObj=eval('('+data+')');	
			if (cboObj != null){
				$.each(cboObj, function(index, text) {
					
					$("#cboClass").append(
							$('<option></option>').val(text).html(index)
					);
					 
				});
			//Load Room from Class (initial)
			LoadRoom(1);	 
			//Load Data Initail
			//LoadData('','',1,1,2556,1);
			}
		});

	}
	
	$("#cboClass_Search").change(function(){
		var cid=$(this).val();
		
		$("#cboRoom").find('option').remove();	
		LoadRoom(cid);
	
	});	
	
	 function LoadRoom(cid){
	 	$.get(url, {"m":"room","i":cid},function(data){
			var cboObj=eval('('+data+')');	
			if (cboObj != null){
				$.each(cboObj, function(index, text) {
					$("#cboRoom").append(
						$('<option></option>').val(text).html(index)
					);
				});
			SearchData();	
			}
		});
	 }
	 function LoadData(code,name,cid,room,year,term){
        $( "#resultData tbody tr" ).remove();
   
        $('#resultData').dataTable( {
			"bProcessing": true,
			"bFilter": false,
			 "bPaginate": false,
			"bLengthChange": false,
			"sAjaxSource": url,
			"oLanguage": {
				"sEmptyTable":     "ไม่พบข้อมูล"
			},
			"bDestroy": true,
			"sServerMethod": "POST",
			"fnServerParams": function ( aoData ) {
					aoData.push( { "name": "m", "value": "load" },
								{"name":"class","value":cid},
								{"name":"room","value":room},
								{"name":"code","value":code},
								{"name":"year","value":year},
								{"name":"term","value":term},
								{"name":"name","value":name}
							);
				},
			"aoColumnDefs":[
			{  "bVisible": false , "aTargets": [ 1 ] },	
			{   "bSortable": true, "aTargets": [ 2 ] ,"sClass":"text-center"},	
			{   "bSortable": true, "aTargets": [ 3 ] ,"sClass":"text-center" },	
			{   "bSortable": true, "aTargets": [ 4 ] ,"sClass":"left" },
			{   "bSortable": true, "aTargets": [ 5 ] ,"sClass":"left" },	
			{	"bSortable": true , "aTargets": [ 6 ],"sClass":"text-center"},	
			{   "aTargets": [ 7 ],
					"sClass":"center",
					"mData": null,
					"mRender": function ( data, type, full ) {
						var cbo_html="";
						 
						if (full[7]>0){
							cbo_html="<img src='img/checkmark1.png'/>";
						}
						return cbo_html;
					}
				},
				{"aTargets": [ 8 ],
					"sClass":"center",
					"mData": null,
					"mRender": function ( data, type, full ) {
						var cbo_html="";
						
						if (full[8]>0){
							cbo_html="<img src='img/checkmark1.png'/>";
						}
						return cbo_html;
					}
				},
				{"aTargets": [ 9 ],
					"sClass":"center",
					"mData": null,
					"mRender": function ( data, type, full ) {
						var cbo_html="";
						if (full[8] > 0 || full[7] > 0){
						 cbo_html="<button type='button' class='btn btn-danger btnregister'>ยกเลิก</button>";
						}
						return cbo_html;
					}
				}
			],"fnDrawCallback": function ( oSettings ) {
				 var that = this;
				/* Need to redo the counters if filtered or sorted */
                 var iTotalRecords = oSettings.fnRecordsTotal();
 
					if (iTotalRecords > 0){ 	
						if ( oSettings.bSorted || oSettings.bFiltered )
						{
							for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
							{
								$('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr ).html( i+1 );
							}
						}
					}
					 
				} 
		 } );
		 
      }
	  
	 $(".cbo_class").live('change',function(){
	     var rowSelect=$(this).parent().parent().parent();
		 
		var cbroom = rowSelect.find("select[name=cbroom]");  
		cbroom.find('option').remove();	
		
		var cid= $(this).val();
		if (cid==0){
			cbroom.append($('<option></option>').val(0).html('--'));
		}else{
			$.post(url,{'m':'room','cid':cid},function(data){
				var obj=eval('('+data+')');	
				if (obj != null){
					$.each(obj, function(index, text) {
						cbroom.append(
								$('<option></option>').val(text).html(index)
						);
						 
					});
				}
			});
		}
	 });
	
	$("#btnCancel").click(function(){
		$('#table_detail').dataTable().fnClearTable();
		objSelectTable=[]
	});
	$("#btnSearch").click(function(){
		
		 SearchData();
	});
	function SearchData(){
		var code = $("#txtCode").val();
		var name = $("#txtName").val();
		var cid = $("#cboClass").val();
		var room = $("#cboRoom").val();
		var term=$("#txtTerm").val();
		var year = $("#cboYear").val();
		 
		LoadData(code,name,cid,room,year,term);
	}
  
	$(".btnregister").live('click',function(){
		var rows = $('#resultData').dataTable().fnGetPosition($(this).closest("tr").get(0));
        var data = $('#resultData').dataTable().fnGetData(rows);

        m_id = data[1];
		$("#m_id").val(m_id);
		$("#m_year").val($("#cboYear").val());
		$("#m_term").val($("#txtTerm").val());
		$("#m_flag").val("1");
		$("#frmRegist").submit();
	});		
	 
	$("#btnPrintDraft").click(function(){
	  $("#resultData").printThis();
		return false;
           
	}); 
  
    
});


